package com.itany.boot.controller;

import com.github.pagehelper.PageInfo;
import com.itany.boot.constant.ResponseCodeConstant;
import com.itany.boot.entity.Comment;
import com.itany.boot.entity.User;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.CommentService;
import com.itany.boot.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @date 2025/10/13
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/findByParams")
    public ModelAndView findByParams(Comment commentParam, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        PageInfo<Comment> commentPage = commentService.findByParams(commentParam, pageNo, pageSize);
        mav.addObject("commentPage", commentPage);
        mav.setViewName("back_commentManageSet");
//        条件参数带回前端
        mav.addObject("commentParam", commentParam);
        return mav;
    }
    @RequestMapping("/findById")
    public ResponseResult findById(String id){
        System.out.println("Received request for user ID: " + id); // 添加日志
        ResponseResult result=new ResponseResult();

        try {
            Comment comment=commentService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
//            设置响应数据
            result.setReturnObject(comment);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id,String status){
        ResponseResult result=new ResponseResult();
        try {
            commentService.modifyStatus(id,status);
            result.setMessage("修改成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;

    }


}
